//
//  DYHomeShopListCell.m
//  TerritoryShopkeeper
//
//  Created by 黄德玉 on 2018/7/19.
//  Copyright © 2018年 ZDQK. All rights reserved.
//

#import "DYHomeShopListCell.h"
#import <Masonry/Masonry.h>

#import <SDWebImage/UIButton+WebCache.h>

@interface DYHomeShopListCell ()

@property (nonatomic,strong) UIImageView * shopLogo;
@property (nonatomic,strong) UIImageView * vipLogo;
@property (nonatomic,strong) UILabel * shopName;
@property (nonatomic,strong) UIButton * enterShopBtn;
@property (nonatomic,strong) UIButton * firstGoods;
@property (nonatomic,strong) UILabel * firstPrice;
@property (nonatomic,strong) UIButton * secondGoods;
@property (nonatomic,strong) UILabel * secondPrice;
@property (nonatomic,strong) UIButton * thirdGoods;
@property (nonatomic,strong) UILabel * thirdPrice;

@end

@implementation DYHomeShopListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor colorWithHex:0xf0f3f5 alpha:1];
        self.contentView.backgroundColor = [UIColor colorWithHex:0xf0f3f5 alpha:1];
        
        _shopLogo = [[UIImageView alloc] initWithImage:PlaceholderImageSmall];
        _shopLogo.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:self.shopLogo];
        
        _vipLogo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"VIP"]];
        [self addSubview:_vipLogo];
        
        _shopName = [UILabel new];
        _shopName.text = @"澳大利亚红酒专区";
        _shopName.textColor = [UIColor blackTextColor];
        _shopName.font = [UIFont systemFontOfSize:15];
        [self addSubview:self.shopName];
        
        _enterShopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_enterShopBtn setImage:[UIImage imageNamed:@"entershop"] forState:UIControlStateNormal];
        _enterShopBtn.userInteractionEnabled  = NO;
        [self addSubview:self.enterShopBtn];
        
        _firstGoods = [UIButton buttonWithType:UIButtonTypeCustom];
        //        _firstGoods.backgroundColor = [UIColor redColor];
        _firstGoods.userInteractionEnabled = NO;
        _firstGoods.layer.borderColor = [[UIColor lightGrayColor] colorWithAlphaComponent:.5].CGColor;
        _firstGoods.layer.borderWidth = .5;
        [self addSubview:self.firstGoods];
        
        _secondGoods = [UIButton buttonWithType:UIButtonTypeCustom];
        //        _secondGoods.backgroundColor = [UIColor blueColor];
        _secondGoods.userInteractionEnabled = NO;
        _secondGoods.layer.borderColor = [[UIColor lightGrayColor] colorWithAlphaComponent:.5].CGColor;
        _secondGoods.layer.borderWidth = .5;
        [self addSubview:self.secondGoods];
        
        _thirdGoods = [UIButton buttonWithType:UIButtonTypeCustom];
        //        _thirdGoods.backgroundColor = [UIColor greenColor];
        _thirdGoods.userInteractionEnabled = NO;
        _thirdGoods.layer.borderColor = [[UIColor lightGrayColor] colorWithAlphaComponent:.5].CGColor;
        _thirdGoods.layer.borderWidth = .5;
        [self addSubview:self.thirdGoods];
        
        _firstPrice = [UILabel new];
        _firstPrice.text = @"￥-";
        _firstPrice.textColor = [UIColor redTextColor];
        _firstPrice.font = [UIFont systemFontOfSize:14];
        _firstPrice.textAlignment = NSTextAlignmentCenter;
        [_firstPrice setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        _firstPrice.numberOfLines = 1;
        [self addSubview:_firstPrice];
        
        _secondPrice   = [UILabel new];
        _secondPrice.text = @"￥-";
        _secondPrice.textColor = [UIColor redTextColor];
        _secondPrice.font = [UIFont systemFontOfSize:14];
        _secondPrice.textAlignment = NSTextAlignmentCenter;
        [_secondPrice setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        _secondPrice.numberOfLines = 1;
        [self addSubview:self.secondPrice];
        
        _thirdPrice = [UILabel new];
        _thirdPrice.text = @"￥-";
        _thirdPrice.textColor = [UIColor redTextColor];
        _thirdPrice.font = [UIFont systemFontOfSize:14];
        _thirdPrice.textAlignment = NSTextAlignmentCenter;
        [_thirdPrice setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        _thirdPrice.numberOfLines = 1;
        [self addSubview:self.thirdPrice];
        
        [self _layout];
    }
    return self;
}

- (void)_layout{
    UIView * roundBgView = [UIView new];
    roundBgView.backgroundColor = [UIColor whiteColor];
//    roundBgView.layer.cornerRadius = 5;
//    roundBgView.layer.masksToBounds = YES;
    [self insertSubview:roundBgView belowSubview:self.shopLogo];
    [roundBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
//        make.height.equalTo(@315).priorityLow();
    }];
    [self.shopLogo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(roundBgView).offset(10);
        make.top.equalTo(roundBgView).offset(10);
        make.width.equalTo(@60);
        make.height.equalTo(@21);
    }];
    [self.vipLogo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.shopLogo);
        make.top.equalTo(self.shopLogo.mas_bottom).offset(4);
    }];
    [self.shopName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.shopLogo.mas_right).offset(5);
        make.centerY.equalTo(self.shopLogo);
        make.right.equalTo(roundBgView).offset(-100);
    }];
    [self.enterShopBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(roundBgView).offset(15);
        make.right.equalTo(roundBgView).offset(-9);
    }];
    
    [self.firstGoods mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(roundBgView).offset(15);
        make.top.equalTo(_enterShopBtn.mas_bottom).offset(21);
        make.width.height.equalTo(@(ScreenWidth / 3.0 - 20));
    }];
    [self.firstPrice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.firstGoods.mas_bottom).offset(5);
        make.right.equalTo(self.firstGoods);
    }];
    [self.secondGoods mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.firstGoods.mas_right).offset(15);
        make.top.equalTo(_enterShopBtn.mas_bottom).offset(21);
        make.width.height.equalTo(@(ScreenWidth / 3.0 - 20));
    }];
    [self.secondPrice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.secondGoods.mas_bottom).offset(5);
        make.right.equalTo(self.secondGoods);
    }];
    [self.thirdGoods mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.secondGoods.mas_right).offset(15);
        make.top.equalTo(_enterShopBtn.mas_bottom).offset(21);
        make.width.height.equalTo(@(ScreenWidth / 3.0 - 20));
    }];
    [self.thirdPrice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.thirdGoods.mas_bottom).offset(5);
        make.right.equalTo(self.thirdGoods);
    }];
    UIView * forthLine = [UIView new];
    forthLine.backgroundColor = [UIColor colorWithHex:0xf0f3f5 alpha:1];
    [self addSubview:forthLine];
    [forthLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(roundBgView);
        make.top.equalTo(self.firstPrice.mas_bottom).offset(10);
        make.height.equalTo(@5);
        make.bottom.equalTo(self).priorityLow();
    }];
}

- (void)loadData:(DYShopModel *)data{
    NSURL * url = [NSURL URLWithString:data.simg];
    [self.shopLogo sd_setImageWithURL:url placeholderImage:PlaceholderImageSmall];
    self.shopName.text = data.sname;
    
    if (data.goods.count > 0) {
        GoodsListModel * model = [GoodsListModel mj_objectWithKeyValues:data.goods[0]];
        NSString * imgUrl = model.ghomeimg;
        [self.firstGoods sd_setBackgroundImageWithURL:[NSURL URLWithString:imgUrl] forState:UIControlStateNormal placeholderImage:PlaceholderImageBig];
        self.firstPrice.text = [NSString stringWithFormat:@"￥%@",model.gprice];
    } else {
        [self.firstGoods setBackgroundImage:PlaceholderImageBig forState:UIControlStateNormal];
        self.firstPrice.text = @"";
    }
    if (data.goods.count > 1) {
        GoodsListModel * model = [GoodsListModel mj_objectWithKeyValues:data.goods[1]];
        NSString * imgUrl = model.ghomeimg;
        [self.secondGoods sd_setBackgroundImageWithURL:[NSURL URLWithString:imgUrl] forState:UIControlStateNormal placeholderImage:PlaceholderImageBig];
        self.secondPrice.text = [NSString stringWithFormat:@"￥%@",model.gprice];
    } else {
        [self.secondGoods setBackgroundImage:PlaceholderImageBig forState:UIControlStateNormal];
        self.secondPrice.text = @"";
    }
    if (data.goods.count > 2) {
        GoodsListModel * model = [GoodsListModel mj_objectWithKeyValues:data.goods[2]];
        NSString * imgUrl = model.ghomeimg;
        [self.thirdGoods sd_setBackgroundImageWithURL:[NSURL URLWithString:imgUrl] forState:UIControlStateNormal placeholderImage:PlaceholderImageBig];
        self.thirdPrice.text = [NSString stringWithFormat:@"￥%@",model.gprice];
    } else {
        [self.thirdGoods setBackgroundImage:PlaceholderImageBig forState:UIControlStateNormal];
        self.thirdPrice.text = @"";
    }
    if (data.isvip) {
        self.vipLogo.hidden = NO;
    }else{
        self.vipLogo.hidden = YES;
    }
}

@end
